Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 26, 2025

  • Understand the issue: <resolv.h> defines _res macro that conflicts with user code variable names
  • Locate the include of <resolv.h> at line 259 in httplib.h within #ifdef __linux__ block
  • Add #undef _res after #include <resolv.h> to prevent macro conflicts
  • Add inline comment explaining why #undef _res is necessary
  • Verify the fix compiles correctly with a test case
  • Run code review and codeql checker
Original prompt

Problem

On Linux, when httplib.h is included, it includes <resolv.h> which defines a macro _res:

// From /usr/include/resolv.h
#define _res (*__res_state())

This causes compilation errors when user code uses _res as a variable name. For example:

#include <iostream>
#include <string>
#include "httplib.h"

int func(int &_res){  // Error: cannot declare pointer to 'int&'
    return 0;
}

int main(int argc, char *argv[])
{
    return 0;
}

The error occurs because the preprocessor expands _res to (*__res_state()), resulting in invalid syntax like int &(*__res_state()).

Solution

Add #undef _res after including <resolv.h> in httplib.h. This is safe because:

  1. httplib.h does not use the _res macro internally
  2. The resolver functions (res_init(), res_query(), etc.) work independently of this macro
  3. This allows user code to use _res as a variable name without conflicts

Implementation

In httplib.h, after the #include <resolv.h> line (around line 259), add:

#ifdef __linux__
#include <resolv.h>
#ifdef _res
#undef _res
#endif
#endif

References

This pull request was created as a result of the following prompt from Copilot chat.

Problem

On Linux, when httplib.h is included, it includes <resolv.h> which defines a macro _res:

// From /usr/include/resolv.h
#define _res (*__res_state())

This causes compilation errors when user code uses _res as a variable name. For example:

#include <iostream>
#include <string>
#include "httplib.h"

int func(int &_res){  // Error: cannot declare pointer to 'int&'
    return 0;
}

int main(int argc, char *argv[])
{
    return 0;
}

The error occurs because the preprocessor expands _res to (*__res_state()), resulting in invalid syntax like int &(*__res_state()).

Solution

Add #undef _res after including <resolv.h> in httplib.h. This is safe because:

  1. httplib.h does not use the _res macro internally
  2. The resolver functions (res_init(), res_query(), etc.) work independently of this macro
  3. This allows user code to use _res as a variable name without conflicts

Implementation

In httplib.h, after the #include <resolv.h> line (around line 259), add:

#ifdef __linux__
#include <resolv.h>
#ifdef _res
#undef _res
#endif
#endif

References


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits November 26, 2025 00:35
Co-authored-by: yhirose <357397+yhirose@users.noreply.github.com>
Co-authored-by: yhirose <357397+yhirose@users.noreply.github.com>
Co-authored-by: yhirose <357397+yhirose@users.noreply.github.com>
Copilot AI changed the title [WIP] Add undef for _res macro in httplib.h Add #undef _res after including resolv.h to prevent macro conflicts Nov 26, 2025
Copilot finished work on behalf of yhirose November 26, 2025 00:38
Copilot AI requested a review from yhirose November 26, 2025 00:38
@yhirose yhirose marked this pull request as ready for review November 26, 2025 00:48
Copy link
Owner

@yhirose yhirose left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an inline source comment after #undef _res to explain why it is necessary.

Co-authored-by: yhirose <357397+yhirose@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 26, 2025

Please add an inline source comment after #undef _res to explain why it is necessary.

Added the inline comment in commit a34d8d8.

Copilot AI requested a review from yhirose November 26, 2025 00:59
Copilot finished work on behalf of yhirose November 26, 2025 00:59
@yhirose yhirose merged commit 9e7861b into master Nov 26, 2025
21 of 24 checks passed
@yhirose yhirose deleted the copilot/undef-res-macro-in-httplib branch November 26, 2025 03:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants